package com.hongsheng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hongsheng.pojo.Bill;
import com.hongsheng.pojo.R;
import com.hongsheng.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:BillController
 * Description:
 *
 * @Author: 李媛芝红豆西米露
 * @Create: 2023/8/10 - 18:43
 * @Version: 2023.1.2
 */
@CrossOrigin
@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private BillService billService;
//    @GetMapping
//    public R findAll(){
////        List<Bill> all = billService.selectAll();
////        return all;
////        return new R(true,billService.selectAll(1,6));
//        return new R(true,billService.list());
//    }
    @GetMapping("{id}")
    public R findById(@PathVariable Integer id){
        return new  R(true,billService.findBillById(id));
    }
    @GetMapping("{type}")
    public R findById(@PathVariable String type){
        return new  R(true,billService.findBillByType(type));
    }
    @GetMapping("/{amount1}/{amount2}")
    public R findByAmount(@PathVariable Integer amount1,@PathVariable Integer amount2){
        return new  R(true,billService.findByAmount(amount1,amount2));
    }
    @PostMapping
    public R addBill(@RequestBody Bill bill){
        return new R(billService.insertBill(bill));
    }
    @PutMapping
    public R update(@RequestBody Bill bill){
        return new R(billService.updateBill(bill));
    }
    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(billService.deleteBill(id));
    }
    @PostMapping ("{currentPage}/{size}/{minAmount}/{maxAmount}")
    public R getBillPage(@PathVariable int currentPage,@PathVariable int size,@PathVariable int minAmount,@PathVariable int maxAmount,@RequestBody Bill bill){
        System.out.println(bill);

        IPage<Bill> page = billService.getPage(currentPage,size,bill,minAmount,maxAmount);
        //当前页大于总页数时出现bug
        if(currentPage>page.getPages()){
            //以总页数再次查询一次
            page=billService.getPage((int) page.getPages(), size,bill,minAmount,maxAmount);

        }
        return new R(true,page);
    }

    /**
     * 查询账单收入
     * @return
     */
    @GetMapping("/selectBillByMonth")
    public R selectOrderByMonth(){
        return new R(true,billService.selectBillByMonth());
    }

    /**
     * 查询账单支出
     * @return
     */
    @GetMapping("/selectBillByMoney")
    public R selectBillByMoney(){
        return new R(true,billService.selectBillByMoney());
    }
}
